# ======================================================================
#
# Copyright (C) ADN
# ADN::LANG::Lang Lang.pm
# Description:
#  - Gestion des langues pour la sonde
#  - Manage the langage of the Sonde
#
# ======================================================================

package ADN::LANG::Lang;

use strict;
use vars qw($AUTOLOAD);
use ADN::XML::XMLtoOBJ;
use ADN::XML::OBJtoXML;
use ADN::CONF::Read;
use File::Path;

# #######################
# CREATION OF THE OBJECT SELF AND INIT
# #######################
sub new
{
	my $self = shift;
  	my $class = ref($self) || $self;
  	return $self if ref $self;
  	$self = bless {} => $class;
  	if ( defined $self->_init() and ($self->_init()) =~ m/^[-]1$/sxm ) {
    		return -1;
  	}
  	return $self;
}

# #######################
# FUNCTION INITIALISATION
# #######################
sub _init
{
  	my $self = shift;
  	my $conf = ADN::CONF::Read->new();
  	if ( defined $conf ) {
    		if ( $conf =~ m/^[-]1$/sxm ) {
      			return -1;
    		}
    		else {
      			my %lang = $conf->lang();
      			$self->{lang_path} = $lang{lang_path};
      			$self->{lang_file} = $lang{lang_file};
      			$self->{path_inc} = $conf->_send_path();
      			undef %lang;
		    }
    		undef $conf;
  	}
  	return;
}

# #######################
# FUNCTION READ THE FILE
# #######################
sub file_xml
{
	my $self = shift;
	my $balise = shift;
	my ( $path_inc, $file, $l1 );
	my @path_inc2;

    if ( $^O eq 'MSWin32' ) {
		  $path_inc = ADN::XML::XMLtoOBJ->new( $ENV{CommonProgramFiles}.'/ADN/path_inc.xml' );
  	}
  	elsif ( $^O eq 'linux' || $^O eq 'darwin' ) {
		  $path_inc = ADN::XML::XMLtoOBJ->new( '/tmp/ADN/path_inc.xml' );
  	}
	  @path_inc2 = $path_inc->conf_inc();
  	$file = $path_inc2[0].$self->{lang_path}.$self->{lang_file};
  	if (-e $file && -f $file && -r $file) {
    		if( open(FILE, '<', $file) ) {
      			while ( $l1 = <FILE> ) {
				      $self->{xml} .= $l1;
      			}
      			if (!close FILE) {}
    		}
  	}
  	$l1 = $file = $path_inc = undef;
  	undef @path_inc2;
  	return $self->division_xml( $balise );
}

# #######################
# FUNCTION PUT IN PLACE THE XML
# #######################
sub division_xml
{
  	my $self = shift;
  	my $balise = shift;
  	$self->{xml} =~ s/(\<[^\>]+\>)[^\<&^\>]*(\<\/[^\>]+\>)/$1$2/gsmx;
  	$self->{xml} =~ s/\s|\n|\r//gsmx;
  	$self->{xml} =~ s/^\<[^\>]+\>(.*)\<[^\>]+\>$/$1/smx;
  	$self->{xml} =~ s/^.*\<$balise\>(.*)\<\/$balise\>.*$/$1/smx;
  	$self->{xml} =~ s/\<\/[^\>]+\>//gsmx;
  	$self->{xml} =~ s/^\<|\>$//gsmx;
  	@{$self->{xml1}} = split/\>\</smx, $self->{xml};
    undef $balise;
  	return ( @{$self->{xml1}} );
}


# #######################
# FUNCTION DESTROY
# #######################
sub DESTROY
{
  	my $self = shift;
  	undef $self;
  	return;
}

# #######################
# FUNCTION AUTOLOAD
# #######################
sub AUTOLOAD
{
  	my $self = shift;
  	my $balise = $AUTOLOAD;
  	$balise =~ s/.*:://smx;
 	  my %hach;
	  my $cnt = 1;
  	my @temp;
  	if (defined $balise ) {
    		if ( $balise =~ m/^[a-zA-Z0-9\_]+$/smx ) {
      			if ( defined $self->{path_inc} and defined $self->{lang_path} and defined $self->{lang_file} ) {
        			my $xml = ADN::XML::XMLtoOBJ->new( $self->{path_inc}.$self->{lang_path}.$self->{lang_file} );
        			if ( (defined $xml and ($xml =~ m/^[-]1$/sxm) ) or ( defined $xml->$balise() and ($xml->$balise() =~ m/^[-]1$/sxm) ) ) {
          				return -1;
        			}
        			my @result = $xml->$balise();
        			$self->file_xml($balise);
        			foreach my $l ( @{$self->{xml1}} ) {
          				@temp = $result[0]->$l();
          				$hach{$l} = $temp[0];
        			}
        			return %hach;
      			}
    		}
  	}
}
1;
__END__

